package com.ponxu.mb.web.action.admin;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ponxu.log.Log;
import com.ponxu.log.LogFactory;
import com.ponxu.mb.model.Link;
import com.ponxu.mb.service.LinkService;
import com.ponxu.mb.web.action.MiniBlogAction;
import com.ponxu.web.core.WebContext;

/**
 * 友情链接 管理
 * 
 * @author xwz
 */
public class LinkAction extends MiniBlogAction {
	private static final Log LOG = LogFactory.getLog();
	private static final String FTL_LINK = "/admin/link.ftl";

	/** 默认查询 */
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		WebContext context = new WebContext(request, response);
		int id = context.getIntParameter("id");
		LOG.debug("id=%d", id);

		List<Link> list = LinkService.searchAll();
		Link link = null;

		if (id > 0) {
			link = LinkService.getById(id);
		} else {
			link = new Link();
			link.setName("");
			link.setDescription("");
			link.setUrl("http://");
			link.setTarget("_blank");
			link.setStatus("visible");
		}

		context.put("list", list);
		context.put("link", link);
		request.setAttribute("status", LinkService.LINK_STATUS);

		return FTL_LINK;
	}

	/**
	 * 保存
	 */
	public String save(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		WebContext context = new WebContext(request, response);
		String msg = "保存成功!";

		int id = context.getIntParameter("id");
		String name = context.getParameter("name");
		String description = context.getParameter("description");
		String url = context.getParameter("url");
		String target = context.getParameter("target");
		String status = context.getParameter("status");

		Link link = null;
		if (id > 0) {
			link = LinkService.getById(id);
		} else {
			link = new Link();
		}

		link.setName(name);
		link.setDescription(description);
		link.setUrl(url);
		link.setTarget(target);
		link.setStatus(status);

		int i = 0;
		if (id > 0) {
			i = LinkService.update(link);
		} else {
			i = LinkService.add(link);
		}

		if (i == 0) {
			msg = "保存失败!";
		}

		context.put("msg", msg);
		context.forward("/admin/link?oper=&id=");
		return null;
	}

	private int[] getIds(HttpServletRequest request) {
		String[] idstr = request.getParameterValues("id");
		int size = idstr.length;
		int[] ids = new int[size];
		for (int i = 0; i < size; i++) {
			ids[i] = Integer.parseInt(idstr[i]);
		}
		return ids;
	}

	/**
	 * 删除
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String delete(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		WebContext context = new WebContext(request, response);
		int[] ids = getIds(request);
		LinkService.deleteById(ids);

		context.put("msg", "删除成功!");
		context.forward("/admin/link?oper=&id=");
		return null;
	}
}